package net.caixiaomi.info.ui.discovery;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.util.DisplayMetrics;
import android.view.View;

import net.caixiaomi.info.R;
import net.caixiaomi.info.app.CommonApp;
import net.caixiaomi.info.base.BaseActivity;
import net.caixiaomi.info.ui.main.DiscoveryFragment;
import net.caixiaomi.info.ui.main.FindFragment;
import net.caixiaomi.info.ui.main.HomeFragment;
import net.caixiaomi.info.ui.main.MatchFragment;
import net.caixiaomi.info.ui.main.UserFragment;

import java.util.List;

/**
 * Created by mac on 2018/9/5.
 */

public class FindActivity extends BaseActivity{

    private FragmentManager fragmentManager;

    @Override
    protected int layout() {
        return R.layout.act_main;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        findViewById(R.id.main_tab_bottom).setVisibility(View.GONE);
        DisplayMetrics metrics = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(metrics);
        fragmentManager=getSupportFragmentManager();
        CommonApp.mScreenWidth=metrics.widthPixels;
        CommonApp.mScreenHeight=metrics.heightPixels;
        if (savedInstanceState == null) {
            getSupportFragmentManager()
                    .beginTransaction()
                    .replace(R.id.container, HomeFragment.newInstance(), HomeFragment.TAG)
                    .commit();
        }
        findViewById(R.id.main_tab_bottom).post(new Runnable() {
            @Override
            public void run() {
                setSelection();
            }
        });

    }

    @Nullable
    public static Fragment getFragmentByTag(@NonNull FragmentManager fragmentManager, @NonNull String tag) {
        return fragmentManager.findFragmentByTag(tag);
    }

    @Nullable
    public static Fragment getVisibleFragment(@NonNull FragmentManager manager) {
        @SuppressLint("RestrictedApi") List<Fragment> fragments = manager.getFragments();
        if (fragments != null && !fragments.isEmpty()) {
            for (Fragment fragment : fragments) {
                if (fragment != null && fragment.isVisible()) {
                    return fragment;
                }
            }
        }
        return null;
    }
    public void onShowHideFragment(@NonNull FragmentManager fragmentManager, @NonNull Fragment toShow, @NonNull Fragment toHide) {
        fragmentManager
                .beginTransaction()
                .hide(toHide)
                .show(toShow)
                .commit();
    }

    public void onAddAndHide(@NonNull FragmentManager fragmentManager, @NonNull Fragment toAdd, @NonNull Fragment toHide) {
        fragmentManager
                .beginTransaction()
                .hide(toHide)
                .add(R.id.container, toAdd, toAdd.getClass().getSimpleName())
                .commit();
    }

    private void setSelection(){
        try {
            Fragment currentVisible = getVisibleFragment(fragmentManager);
            FindFragment findFragment = (FindFragment) getFragmentByTag(fragmentManager, FindFragment.TAG);
            if (findFragment == null) {
                onAddAndHide(fragmentManager, FindFragment.newInstance(), currentVisible);
            } else {
                onShowHideFragment(fragmentManager, findFragment, currentVisible);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}
